package indi.zhifa.recipe.bailan.busy.auth.dao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import indi.zhifa.recipe.bailan.busy.auth.entity.po.DeviceGroupEntity;
import indi.zhifa.recipe.bailan.busy.auth.dao.mapper.DeviceGroupMapper;
import indi.zhifa.recipe.bailan.busy.auth.dao.service.IDeviceGroupDbService;
import indi.zhifa.recipe.bailan.framework.mysql.entity.annotations.ZfDbService;
import indi.zhifa.recipe.bailan.framework.mysql.service.impl.ZfDbServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;

/**
 * <p>
 * 设备群组表，用于为群组下的设备制定统一的规则，管理设备 服务实现类
 * </p>
 *
 * @author 织法
 * @since 2023-01-08
 */
@CacheConfig(cacheNames = "device_group-byAuthCode")
@ZfDbService(name = "DeviceGroupEntity")
public class DeviceGroupDbServiceImpl extends ZfDbServiceImpl<DeviceGroupMapper, DeviceGroupEntity> implements IDeviceGroupDbService {


    @CacheEvict(key = "#pAuthId")
    @Override
    public DeviceGroupEntity deleteByAuthId(Long pAuthId) {
        LambdaQueryWrapper<DeviceGroupEntity> queryWrapper = Wrappers.<DeviceGroupEntity>lambdaQuery()
                .eq(DeviceGroupEntity::getAuthId,pAuthId);
        DeviceGroupEntity entity = findOne(queryWrapper);
        if(null != entity){
            removeById(entity.getId());
        }
        return entity;
    }

    @Cacheable(key = "#pAuthId",condition = "null != #result")
    @Override
    public DeviceGroupEntity findByAuthId(Long pAuthId){
        LambdaQueryWrapper<DeviceGroupEntity> queryWrapper = Wrappers.<DeviceGroupEntity>lambdaQuery()
                .eq(DeviceGroupEntity::getAuthId,pAuthId);
        DeviceGroupEntity entity = findOne(queryWrapper);
        return entity;
    }

}
